package cn.com.ejiakanghu.dto;

import com.alibaba.fastjson.JSONObject;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.jsonfield.LitemallGoodsProductSpecification;
import org.linlinjava.litemall.db.vo.app.GoodsVipSalesDto;
import org.linlinjava.litemall.db.vo.app.ServiceProductVo;
import org.linlinjava.litemall.db.vo.app.StoreGoodsProductVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

public class GoodsSkuItemInfo {

    /**
     * SKU商品ID，唯一
     */
    private Integer skuId;
    /**
     * SKU商品规格ID数组，例如: [1002,2002,3001]
     */
    private List<Integer> specIds;
    /**
     * SKU商品图片URL，图片宽高尺寸比例320x320
     */
    private String image;
    /**
     * SKU商品剩余库存数量
     */
    private int stock;
    /**
     * SKU商品价格信息
     */
    private GoodsSkuItemPriceInfo priceInfo;

    private List<GoodsVipSalesDto> vipSales;

    public List<GoodsVipSalesDto> getVipSales() {
        return vipSales;
    }

    public void setVipSales(List<GoodsVipSalesDto> vipSales) {
        this.vipSales = vipSales;
    }

    public Integer getSkuId() {
        return skuId;
    }

    public void setSkuId(Integer skuId) {
        this.skuId = skuId;
    }

    public List<Integer> getSpecIds() {
        return specIds;
    }

    public void setSpecIds(List<Integer> specIds) {
        this.specIds = specIds;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public GoodsSkuItemPriceInfo getPriceInfo() {
        return priceInfo;
    }

    public void setPriceInfo(GoodsSkuItemPriceInfo priceInfo) {
        this.priceInfo = priceInfo;
    }

    public static GoodsSkuItemInfo getInstance(StoreGoodsProductVo product, LitemallGoods goods) {
        GoodsSkuItemInfo itemInfo = new GoodsSkuItemInfo();
        itemInfo.setSkuId(product.getId());
        itemInfo.setImage(product.getUrl());
        itemInfo.setStock(product.getNumber());
        List<LitemallGoodsProductSpecification> specificationList = JSONObject.parseArray(product.getSpecifications(), LitemallGoodsProductSpecification.class);
        List<Integer> specIdList = specificationList.stream().map(p -> p.getId()).collect(Collectors.toList());
        itemInfo.setSpecIds(specIdList);

        GoodsSkuItemPriceInfo priceInfo = new GoodsSkuItemPriceInfo();
        priceInfo.setPrice(null == product.getSaleRetailPrice() ? null : product.getSaleRetailPrice().doubleValue());
        priceInfo.setLinePrice(goods.getCounterPrice().doubleValue());
        priceInfo.setSuffix(goods.getUnit());
        priceInfo.setDeposit(null == product.getLeaseDeposit() ? null : product.getLeaseDeposit().doubleValue());

        itemInfo.setPriceInfo(priceInfo);
        return itemInfo;
    }

    public static GoodsSkuItemInfo getInstance(ServiceProductVo product) {
        GoodsSkuItemInfo itemInfo = new GoodsSkuItemInfo();
        itemInfo.setSkuId(product.getId());
        itemInfo.setImage(product.getUrl());
        List<LitemallGoodsProductSpecification> regualrForms = JSONObject.parseArray(product.getSpecifications(), LitemallGoodsProductSpecification.class);
        List<Integer> specIdList = regualrForms.stream().map(p -> p.getId()).collect(Collectors.toList());
        itemInfo.setSpecIds(specIdList);

        GoodsSkuItemPriceInfo priceInfo = new GoodsSkuItemPriceInfo();
        BigDecimal price = null != product.getCityProductPrice() ? product.getCityProductPrice() : product.getPrice();
        priceInfo.setPrice(null != price ? price.doubleValue() : null);
        priceInfo.setLinePrice(null == product.getOriginalPrice() ? null : product.getOriginalPrice().doubleValue());
        priceInfo.setSuffix("次");
        itemInfo.setPriceInfo(priceInfo);
        return itemInfo;
    }
}

